package cn.rxls.community.controller;


import cn.rxls.community.dto.goods.AddOrdersGoodsDTO;
import cn.rxls.community.dto.goods.GoodsPageDTO;
import cn.rxls.community.result.Result;
import cn.rxls.community.vo.goods.GoodsVO;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.rxls.community.entity.Goods;
import cn.rxls.community.service.GoodsService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.annotation.WebFilter;
import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.List;

/**
 * 商品(Goods)表
 *
 */
@RestController
@RequestMapping("goods")
@Validated
public class GoodsController {
    /**
     * 服务对象
     */
    @Resource
    private GoodsService goodsService;

    /**
     * 根据分类分页查询数据
     *
     * @param dto 分页对象
     * @return 所有数据
     */
    @GetMapping("/page/state")
    public Result<List<GoodsVO>> selectPageByCategory(@ModelAttribute @Valid GoodsPageDTO dto) {



        return Result.success(goodsService.selectPageByCategory(dto));
    }

    /**
     * 根据id查询数据
     *
     * @param id 商品主键id
     * @return 单条数据
     */
    @GetMapping("/detail")
    public Result<GoodsVO> selectDetailId(@RequestParam(value = "id", required = false)
                                                @NotNull(message = "非法id")
                                                @Min(value = 1, message = "非法id") Long id) {
        return Result.success(goodsService.selectDetail(id));
    }

    /**
     * 商品购买
     * @param dto 参数对象
     * @return 订单id
     */
    @PostMapping("/buy")
    public Result<Long> purchaseGoods (@Valid @RequestBody AddOrdersGoodsDTO dto){
        return Result.success(goodsService.purchaseGoods(dto));
    }


}

